import * as THREE from 'three';
import { ThreeBSP } from 'three-js-csg-es6';

/**
 * 墙
 */
export class Wall extends THREE.Mesh {
  /**
   * 初始化墙
   *
   * @param {Object} options 墙参数
   *
   * @param {Number} options.width 大小长
   * @param {Number} options.height 大小高
   * @param {Number} options.depth 大小深
   * @param {Number} options.x 位置长
   * @param {Number} options.y 位置高
   * @param {Number} options.z 位置深
   */
  constructor(options) {
    const defaultOptions = {
      width: 1000,
      height: 200,
      depth: 20,
      x: 0,
      y: 0,
      z: 0,
      window: {
        width: 500,
        height: 100,
        x: 250,
        y: 50,
        z: 0
      }
    };
    const mergeOptions = Object.assign(defaultOptions, options);
    const { width, height, depth, x, y, z, window } = mergeOptions;
    const wallGeometry = new THREE.BoxGeometry(width, height, depth);
    /**
     * 冯氏材质
     * 一种用于具有镜面高光的光泽表面的材质
     * color 颜色
     * specular 高光颜色
     * transparent 是否透明
     * opacity 透明度
     */
    const wallMaterial = new THREE.MeshPhongMaterial({
      color: '#9cb2d1',
      specular: '#9cb2d1',
      shininess: 30,
      transparent: true,
      opacity: 0.5
    });
    /**
     * 挖窗或门
     */
    if (window) {
      const windowGeometry = new THREE.BoxGeometry(
        window.width,
        window.height,
        depth
      );
      debugger
      const a = new ThreeBSP(wallGeometry);
      const b = new ThreeBSP(windowGeometry);
      debugger;
      const result = a.subtract(b);
      const mesh = result.toMesh();
      super(mesh.geometry, wallMaterial);
    } else {
      super(wallGeometry, wallMaterial);
    }
    this.position.set(x, y, z);
    this.options = mergeOptions;
  }
}
